﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using CTRL;
using DAL;
using System.Threading;

namespace GUI_Administration
{
    public partial class GUIBatteryModel : Form
    {
        private static GUIBatteryModel instance;
        private CtrlBatteryModel ctrlBatMod;
        
        private GUIBatteryModel()
        {
            ctrlBatMod = CtrlBatteryModel.Instance;
            
            InitializeComponent();

            UpdateTable();
        }

        public static GUIBatteryModel Instance
        {
            get
            {
                if (instance == null)
                    instance = new GUIBatteryModel();

                return instance;
            }
        }

        private void UpdateTable()
        {
            dataGridView.Rows.Clear();
            List<BatteryModel> list = ctrlBatMod.GetAllBatteryModels().ToList();
            foreach (BatteryModel model in list)
                dataGridView.Rows.Add(model.modelID, model.model, model.capacity, 
                    model.type, model.manufacturerID);
        }

        private void nameBtn_Click(object sender, EventArgs e)
        {
            if (nameTxtBox.Text.ToString() == "0")
                UpdateTable();
            else
            {
                dataGridView.Rows.Clear();
                BatteryModel _model = ctrlBatMod.GetBatteryModelByName(nameTxtBox.Text.ToString());
                dataGridView.Rows.Add(_model.modelID, _model.model, _model.capacity, 
                    _model.type, _model.manufacturerID);
            }
            nameTxtBox.Clear();
        }

        private void IDbtn_Click(object sender, EventArgs e)
        {
            if (int.Parse(IDtxtBox.Text) == 0)
                UpdateTable();
            else
            {
                dataGridView.Rows.Clear();
                BatteryModel _model = ctrlBatMod.GetBatteryModel(int.Parse(IDtxtBox.Text));
                dataGridView.Rows.Add(_model.modelID, _model.model, _model.capacity,
                    _model.type, _model.manufacturerID);
            }
            IDtxtBox.Clear();
        }

        private void addBtn_Click(object sender, EventArgs e)
        {
            ctrlBatMod.CreateBatteryModel(dataGridView.CurrentRow.Cells[1].Value.ToString(), 
                double.Parse(dataGridView.CurrentRow.Cells[2].Value.ToString()),
                dataGridView.CurrentRow.Cells[3].Value.ToString(),
                int.Parse(dataGridView.CurrentRow.Cells[4].Value.ToString()));
            UpdateTable();
        }

        private void updateBtn_Click(object sender, EventArgs e)
        {
            ctrlBatMod.UpdateBatteryModel(int.Parse(dataGridView.CurrentRow.Cells[0].Value.ToString()),
                dataGridView.CurrentRow.Cells[1].Value.ToString(),
                double.Parse(dataGridView.CurrentRow.Cells[2].Value.ToString()),
                dataGridView.CurrentRow.Cells[3].Value.ToString(),
                int.Parse(dataGridView.CurrentRow.Cells[4].Value.ToString()));
            UpdateTable();
        }

        private void deleteBtn_Click(object sender, EventArgs e)
        {
            ctrlBatMod.DeleteBatteryModel(int.Parse(dataGridView.CurrentRow.Cells[0].Value.ToString()));
            UpdateTable();
        }

        private void back_Click(object sender, EventArgs e)
        {
            GUIMain main = GUIMain.Instance;
            main.Show();

            this.Hide();
        }
    }
}
